package org.sam.swing.table.tools;

import javax.swing.Action;
import javax.swing.JComponent;

import org.jdesktop.swingx.JXFindBar;
import org.sam.swing.JSToolBar;
import org.sam.swing.action.JSFileExportActionListener;
import org.sam.swing.action.JSFileImportActionListener;
import org.sam.swing.table.JSTable;
import org.sam.swing.table.action.JSTableAppendAction;
import org.sam.swing.table.action.JSTableDeleteAction;
import org.sam.swing.table.action.JSTableEditAction;
import org.sam.swing.table.action.JSTableExportAction;
import org.sam.swing.table.action.JSTableFitlerAction;
import org.sam.swing.table.action.JSTableImportAction;
import org.sam.swing.table.action.JSTableInvertSelectAction;
import org.sam.swing.table.action.JSTableRetrieveAction;
import org.sam.swing.table.action.JSTableRullRowSelectAction;
import org.sam.swing.table.action.JSTableRullRowUnSelectAction;
import org.sam.swing.table.action.JSTableSaveAction;
import org.sam.swing.table.action.JSTableSelectAction;
import org.sam.swing.table.action.JSTableUnSelectAction;
import org.sam.swing.table.defaultImpl.DefaultExportListenerActionImpl;
import org.sam.swing.table.defaultImpl.DefaultImportListenerActionImpl;

/**
 * 给JSTable增加的工具条
 * 
 * @author sam
 *
 */
public class JSTableToolBar extends JSToolBar {

	private static final long serialVersionUID = -5793491647918673440L;

	/**
	 * 当前操作的table
	 */
	private JSTable table;

	/**
	 * 当前操作的table
	 * 
	 * @return
	 */
	public JSTable getTable() {
		return table;
	}

	/**
	 * 当前操作的table
	 * 
	 * @param table
	 */
	public void setTable(JSTable table) {
		this.table = table;
	}

	/**
	 * 构造函数
	 * 
	 * @param table
	 * @param tableModel
	 */
	public JSTableToolBar(JSTable table) {
		this.setTable(table);
		this.setBorder(null);
		this.setBorderPainted(false);
		this.setFloatable(false);
		this.setRollover(true);
		this.setOpaque(false);
	}

	/**
	 * 刷新
	 * 
	 * @param retrieve
	 */
	public Action[] enableRetrieve() {
		JSTableRetrieveAction action = new JSTableRetrieveAction(table);
		this.addAction(action);
		return new Action[] { action };
	}

	/**
	 * 全选，不全选 默认操作第一列 select：Integer.Valueof(1) unSelect：Integer.Valueof(0)
	 * 
	 */
	public Action[] enableSelect() {
		JSTableSelectAction selectAction = new JSTableSelectAction(table);
		this.addAction(selectAction);

		JSTableUnSelectAction unSelectAction = new JSTableUnSelectAction(table);
		this.addAction(unSelectAction);

		JSTableInvertSelectAction invertSelectAction = new JSTableInvertSelectAction(table);
		this.addAction(invertSelectAction);

		return new Action[] { selectAction, unSelectAction, invertSelectAction };
	}

	/**
	 * 全选，不全选 select：Integer.Valueof(1) unSelect：Integer.Valueof(0)
	 * 
	 * @param editColIndex 操作列索引
	 */
	public Action[] enableSelect(int editColIndex) {
		JSTableSelectAction selectAction = new JSTableSelectAction(table);
		selectAction.setEditColIndex(editColIndex);
		this.addAction(selectAction);

		JSTableUnSelectAction unSelectAction = new JSTableUnSelectAction(table);
		unSelectAction.setEditColIndex(editColIndex);
		this.addAction(unSelectAction);

		JSTableInvertSelectAction invertSelectAction = new JSTableInvertSelectAction(table);
		invertSelectAction.setEditColIndex(editColIndex);
		this.addAction(invertSelectAction);

		return new Action[] { selectAction, unSelectAction, invertSelectAction };
	}

	/**
	 * 全选，不全选
	 * 
	 * @param editColIndex  操作列索引
	 * @param selectValue   选中值
	 * @param unselectValue 非选中值
	 */
	public Action[] enableSelect(int editColIndex, Object selectValue, Object unselectValue) {
		JSTableSelectAction selectAction = new JSTableSelectAction(table);
		selectAction.setEditColIndex(editColIndex);
		selectAction.setSelectedValue(selectValue);
		this.addAction(selectAction);

		JSTableUnSelectAction unSelectAction = new JSTableUnSelectAction(table);
		unSelectAction.setEditColIndex(editColIndex);
		unSelectAction.setUnSelectedValue(unselectValue);
		this.addAction(unSelectAction);

		JSTableInvertSelectAction invertSelectAction = new JSTableInvertSelectAction(table);
		invertSelectAction.setEditColIndex(editColIndex);
		invertSelectAction.setSelectedValue(selectValue);
		invertSelectAction.setUnSelectedValue(unselectValue);
		this.addAction(invertSelectAction);

		return new Action[] { selectAction, unSelectAction, invertSelectAction };
	}

	/**
	 * 允许整行选中
	 */
	public Action[] enableFullRowSelect() {
		JSTableRullRowSelectAction rullRowSelectAction = new JSTableRullRowSelectAction(table);
		this.addAction(rullRowSelectAction);

		JSTableRullRowUnSelectAction rullRowUnSelectAction = new JSTableRullRowUnSelectAction(table);
		this.addAction(rullRowUnSelectAction);

		return new Action[] { rullRowSelectAction, rullRowUnSelectAction };
	}

	/**
	 * 导入操作
	 * 
	 * @param fileImporListener 回调接口
	 */
	public Action[] enableImp(JSFileImportActionListener fileImporListener) {
		JSTableImportAction importAction = new JSTableImportAction(table);
		importAction.addFileImportActionListener(fileImporListener);
		this.addAction(importAction);
		
		return new Action[] {importAction};
	}

	/**
	 * 导入操作,使用系统默认实现
	 */
	public Action[] enableImp() {
		JSTableImportAction importAction = new JSTableImportAction(table);
		DefaultImportListenerActionImpl l = new DefaultImportListenerActionImpl(table);
		l.setBeginRow(1);
		importAction.addFileImportActionListener(l);
		this.addAction(importAction);
		
		return new Action[] {importAction}; 
	}

	/**
	 * 开启文件导出功能
	 * 
	 * @param fileImporListener
	 */
	public Action[] enableExport(JSFileExportActionListener fileExportListener) {
		JSTableExportAction importAction = new JSTableExportAction(table);
		importAction.addFileExportActionListener(fileExportListener);
		this.addAction(importAction);
		
		return new Action[] {importAction};
	}

	/**
	 * 使用默认的导出功能
	 * 
	 * @param fileName 默认的文件名称
	 */
	public Action[] enableExport(String fileName) {
		JSTableExportAction exportAction = new JSTableExportAction(table);
		exportAction.addFileExportActionListener(new DefaultExportListenerActionImpl(table, fileName));
		this.addAction(exportAction);
		
		return new Action[] {exportAction};
	}

	/**
	 * 编辑
	 * 
	 * @param edit
	 */
	public Action[] enableEdit() {
		JSTableEditAction editAction = new JSTableEditAction(table);
		this.addAction(editAction);
		return new Action[] {editAction};
	}

	/**
	 * 新增
	 * 
	 */
	public Action[] enableAdd() {
		JSTableAppendAction appendAction = new JSTableAppendAction(table);
		this.addAction(appendAction);
		return new Action[] {appendAction};
	}

	/**
	 * 删除
	 * 
	 * @param del
	 */
	public Action[] enableDel() {
		JSTableDeleteAction deleteAction = new JSTableDeleteAction(table);
		this.addAction(deleteAction);
		return new Action[] {deleteAction};
	}

	/**
	 * 保存
	 * 
	 * @param save
	 */
	public Action[] enableSave() {
		JSTableSaveAction saveAction = new JSTableSaveAction(table);
		this.addAction(saveAction);
		return new Action[] {saveAction};
	}

	/**
	 * 允许增加过滤功能
	 */
	public Action[] enableFilter() {
		JSTableFitlerAction fitlerAction = new JSTableFitlerAction(table);
		this.addAction(fitlerAction);
		return new Action[] {fitlerAction};
	}

	/**
	 * 搜索
	 * 
	 * @param search
	 */
	public void enableSearch() {
		this.add(buildSearchBar());
	}

	/**
	 * 使用jxtable自带的searchbar
	 * 
	 * @return
	 */
	public JComponent buildSearchBar() {
		// JSFindBar searchBar = new JSFindBar(table.getSearchable(),
		// table.getModel(), table.getColumnModel());
		JXFindBar searchBar = new JXFindBar(this.getTable().getSearchable());
		return searchBar;
	}
}
